<?php

/**
 * This is the model class for table "user_message".
 *
 * The followings are the available columns in table 'user_message':
 * @property integer $user_id
 * @property integer $user_messaged
 * @property string $datetime_send
 * @property string $text
 * @property integer $draft
 * @property string $read
 * @property string $feature_name
 * @property string $from_page
 */
class mUserMessage extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return mUserMessage the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user_message';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, user_messaged, datetime_send, text, draft, feature_name, from_page', 'required'),
			array('user_id, user_messaged, draft', 'numerical', 'integerOnly'=>true),
			array('read', 'length', 'max'=>6),
			array('feature_name', 'length', 'max'=>100),
			array('from_page', 'length', 'max'=>200),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_id, user_messaged, datetime_send, text, draft, read, feature_name, from_page', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User',
			'user_messaged' => 'User Messaged',
			'datetime_send' => 'Datetime Send',
			'text' => 'Text',
			'draft' => 'Draft',
			'read' => 'Read',
			'feature_name' => 'Feature Name',
			'from_page' => 'From Page',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('user_messaged',$this->user_messaged);
		$criteria->compare('datetime_send',$this->datetime_send,true);
		$criteria->compare('text',$this->text,true);
		$criteria->compare('draft',$this->draft);
		$criteria->compare('read',$this->read,true);
		$criteria->compare('feature_name',$this->feature_name,true);
		$criteria->compare('from_page',$this->from_page,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    public function getUserMessageWithPagination($user_id, $offset, $limit){
      $user = Yii::app()->db->createCommand()
                  ->select('p.*,um.text, um.datetime_send as date_view, u.username, up.link_img, ct.name as country_name, uf.user_favorited')
                  ->from('user_message um')
                  ->leftjoin('profiles p', 'p.user_id=um.user_messaged')
                  ->leftjoin('users u', 'u.user_id=um.user_messaged')
                  ->leftjoin('user_photos up', 'up.approve=1 and up.make_primary=1 and up.user_id=um.user_messaged')
                  ->leftjoin('countries ct', 'ct.code = p.country')
                  ->leftjoin('user_favorite uf', 'uf.user_favorited=um.user_messaged and uf.user_id=:user_id',array(':user_id'=>$user_id))
                  ->where('um.user_id=:user_id',array(':user_id'=>$user_id))
                  ->limit($limit,$offset)
                  ->queryAll();
      return $user;
    }
    
    public function getUserMessagedWithPagination($user_messaged, $offset, $limit){
      $user = Yii::app()->db->createCommand()
                  ->select('p.*,um.text, um.datetime_send as date_view, u.username, up.link_img, ct.name as country_name, uf.user_favorited')
                  ->from('user_message um')
                  ->leftjoin('profiles p', 'p.user_id=um.user_id')
                  ->leftjoin('users u', 'u.user_id=um.user_id')
                  ->leftjoin('user_photos up', 'up.approve=1 and up.make_primary=1 and up.user_id=um.user_id')
                  ->leftjoin('countries ct', 'ct.code = p.country')
                  ->leftjoin('user_favorite uf', 'uf.user_favorited=um.user_id and uf.user_id=:user_id',array(':user_id'=>$user_messaged))
                  ->where('um.user_messaged=:user_messaged',array(':user_messaged'=>$user_messaged))
                  ->limit($limit,$offset)
                  ->queryAll();
      return $user;
    }
}